package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.service.UserService;

public class AddUserController extends HttpServlet{
	UserService userservice=new UserService();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uname=req.getParameter("uname");
		String upsw=req.getParameter("upsw");
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		//获取session对象
		HttpSession session=req.getSession();
		boolean flag=userservice.addUser(uname, upsw);
		if(flag){
			//获取当前用户的对象，放入session中
			session.setAttribute("uname",uname);
			session.setAttribute("upsw", upsw);
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}else{
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}
}
